package com.yfp.machine.controller;

import com.yfp.common.core.domain.R;
import com.yfp.common.core.web.controller.BaseController;
import com.yfp.common.core.web.page.TableDataInfo;
import com.yfp.common.log.annotation.Log;
import com.yfp.common.log.enums.BusinessType;
import com.yfp.machine.domain.vo.MachineAdAddVo;
import com.yfp.machine.domain.vo.MachineAdVo;
import com.yfp.machine.service.MachineAdService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;


/**
 * (MachineAd)表控制层
 *
 *
 * @since 2023-12-16 21:24:24
 */
@RestController
@RequestMapping("machineAd")
@Tag(name = "设备广告")
public class MachineAdController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private MachineAdService machineAdService;

    @Operation(summary = "设备明细--根据设备id查询设备广告列表")
    @GetMapping("adList")
    public R queryMachineAdvertisingListByMachineId(
            @Parameter(name = "machineId") @Valid String machineId)
    {
        return R.ok(this.machineAdService.queryMachineAdvertisingListByMachineId(machineId));
    }

    /**
     * 推送机器广告
     *
     * @param machineAdId 机器广告id
     * @param machineId   设备id
     * @return {@link R}
     */
    @Operation(summary = "设备明细--推送机器广告 -单个")
    @Log(title = "推送机器广告", businessType = BusinessType.UPDATE)
    @PostMapping("push")
    public R push(@Parameter(name = "machineAdId") @Valid String machineAdId,
                  @Parameter(name = "machineId") @Valid String machineId)
    {
        return this.machineAdService.pushAd(machineAdId, machineId) ?
                R.ok(null, "推送成功") : R.fail("推送失败");
    }


    /**
     * 推送机器广告
     *
     * @param machineAdId 机器广告id
     * @return {@link R}
     */
    @Operation(summary = "设备广告--推送机器广告--批量")
    @Log(title = "推送机器广告", businessType = BusinessType.UPDATE)
    @GetMapping("/ad/push")
    public R push(@Parameter(name = "machineAdId") @Valid String machineAdId)
    {
        return this.machineAdService.adPushMachineAd(Long.valueOf(machineAdId)) ?
                R.ok(null, "推送成功") : R.fail("推送失败");
    }

    @GetMapping
    @Operation(summary = "设备广告--分页查询设备广告列表")
    public TableDataInfo queryByPage(MachineAdVo machineAdVo,
                                     @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                     @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize)
    {
        return getDatableByStream(this.machineAdService.queryMachineAdVoByPage(machineAdVo), pageNum, pageSize);
    }

    @GetMapping("/ad/{machineAdId}")
    @Operation(summary = "设备广告--根据设备广告id查询设备广告详情")
    public R queryById(@Valid @PathVariable("machineAdId") String machineAdId)
    {
        return R.ok(this.machineAdService.queryMachineAdInfoById(Long.valueOf(machineAdId)));
    }

    @Operation(summary = "设备广告--新增设备广告")
    @Log(title = "设备广告", businessType = BusinessType.INSERT)
    @PostMapping
    public R insert(@RequestBody @Valid MachineAdAddVo machineAdVo)
    {
        return this.machineAdService.addMachineAd(machineAdVo);
    }


    @Operation(summary = "设备广告--删除设备广告")
    @Log(title = "设备广告", businessType = BusinessType.DELETE)
    @DeleteMapping("{machineAdId}")
    public R delete(@PathVariable @Parameter(name = "machineAdId") String machineAdId)
    {
        return this.machineAdService.deleteMachineAd(Long.valueOf(machineAdId)) ?
                R.ok(null, "删除成功") : R.fail("删除失败");
    }

    @Operation(summary = "设备广告--投放/下架广告  投放-1 下架-0")
    @Log(title = "设备广告-播放/暂停广告", businessType = BusinessType.UPDATE)
    @GetMapping("play")
    public R play(@Parameter(name = "machineAdId") @Valid String machineAdId,
                  @Parameter(name = "palyStatus") @Valid Integer palyStatus)
    {
        return this.machineAdService.playMachineAd(Long.valueOf(machineAdId), palyStatus) ?
                R.ok() : R.fail();
    }
}

